package com.supermap.business.iot;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.supermap.business.iot.service.IotConsumerService;
import com.supermap.shiro.auth.entity.AppUser;
import com.supermap.shiro.common.AuthFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author xiaolu
 * @date 2018/6/8 上午10:28
 */

@RestController
@RequestMapping(value = "iot")
public class IotController {

    private  static final boolean USE_MD5 = false;


        @Resource
        IotConsumerService iotConsumerService;


    /**
     * 获取基地列表
     *
     * @return
     */
    @RequestMapping(value = "getRegions")
    @ResponseBody
    public JSONObject getRegions() {
        AppUser appUser = AuthFactory.getUserSession();
        System.out.println(AuthFactory.getUserSession().getLoginIotName()+AuthFactory.getUserSession().getLoginIotPassword());
        return iotConsumerService.getRegions(
                AuthFactory.getUserSession().getLoginIotName(),
                AuthFactory.getUserSession().getLoginIotPassword(),
                false);
    }

    /**
     * 根据RegionID获取节点列表
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "getNodesByRegionID")
    @ResponseBody
    public JSONObject getNodesByRegionID(@RequestBody Map param) {
        int regionId;
        if (param.get("regionID") instanceof String) {
            regionId = Integer.parseInt((String) param.get("regionID"));
        } else {
            regionId = (int) param.get("regionID");
        }
        return iotConsumerService.getNodesByRegionID(  AuthFactory.getUserSession().getLoginIotName()
                ,  AuthFactory.getUserSession().getLoginIotPassword(), false, regionId);
    }

    /**
     * 获取节点列表
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "getNodesByBMID")
    @ResponseBody
    public JSONObject getNodesByBMID(@RequestBody Map param) {
        int bmId;
        if (param.get("bmId") instanceof String) {
            bmId = Integer.parseInt((String) param.get("bmId"));
        } else {
            bmId = (int) param.get("bmId");
        }
        return iotConsumerService.getNodesByBMID(  AuthFactory.getUserSession().getLoginIotName()
                ,   AuthFactory.getUserSession().getLoginIotPassword(), USE_MD5, bmId);
    }

    /**
     * 获取设备种类
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "getMonitorCategories")
    @ResponseBody
    public JSONObject getMonitorCategories(@RequestBody Map param) {
        int nodeId;
        if (param.get("nodeId") instanceof String) {
            nodeId = Integer.parseInt((String) param.get("nodeId"));
        } else {
            nodeId = (int) param.get("nodeId");
        }
        return iotConsumerService.getMonitorCategories(  AuthFactory.getUserSession().getLoginIotName()
                ,   AuthFactory.getUserSession().getLoginIotPassword()
                , USE_MD5, nodeId);
    }

    /**
     * 获取节点实时数据 不传值时默认获取所有数据
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "getEnvInfoDetails")
    @ResponseBody
    public JSONObject getEnvInfoDetails(@RequestBody Map param) {
        JSONObject nodeData = new JSONObject();
        List nodeIdList = (List) param.get("nodeId");
        AppUser appUser=  AuthFactory.getUserSession();
        nodeData = iotConsumerService.getEnvInfoDetails(  AuthFactory.getUserSession().getLoginIotName(),
                AuthFactory.getUserSession().getLoginIotPassword(), USE_MD5, nodeIdList);


        if (nodeData.keySet().size() > 0) {
            return assistMsg(200, nodeData);
        }
        return assistMsg(404, nodeData);
    }

    /**
     * 获取节点历史数据
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "getEnvInfosByStat")
    @ResponseBody
    public JSONObject getEnvInfosByStat(@RequestBody Map param) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
        int nodeId;
        int type;
        if (param.get("nodeId") instanceof String) {
            nodeId = Integer.parseInt((String) param.get("nodeId"));
        } else {
            nodeId = (int) param.get("nodeId");
        }
        if (param.get("type") instanceof String) {
            type = Integer.parseInt((String) param.get("type"));
        } else {
            type = (int) param.get("type");
        }

        String beginDateStr = (String) param.get("beginDate");
        String endDateStr = (String) param.get("endDate");
        if (endDateStr == null) {
            return iotConsumerService.getEnvInfosByStat(  AuthFactory.getUserSession().getLoginIotName(),
                    AuthFactory.getUserSession().getLoginIotPassword(), USE_MD5, nodeId, beginDateStr, type);
        } else {
            try {
                JSONArray dataArray = new JSONArray();
                Date beginDate = sdf.parse(beginDateStr);
                Date endDate = sdf.parse(endDateStr);
                while (beginDate.before(endDate)) {
                    JSONObject data = iotConsumerService.getEnvInfosByStat(  AuthFactory.getUserSession().getLoginIotName()
                            ,   AuthFactory.getUserSession().getLoginIotPassword()
                            , USE_MD5, nodeId, sdf.format(beginDate), type);
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(beginDate);
                    calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + 1);
                    beginDate = calendar.getTime();
                    dataArray.addAll(data.getJSONArray("data"));
                }
                if (dataArray.size() > 0) {
                    return assistMsg(200, dataArray);
                }
                return assistMsg(404, dataArray);
            } catch (ParseException e) {
                return assistMsg(404, null, "ParseException");
            }
        }
    }

    private JSONObject assistMsg(int errorCode, Object data, String message) {
        JSONObject msg = new JSONObject();
        msg.put("code", errorCode);
        if (errorCode == 404) {
            msg.put("msg", message);
        } else {
            msg.put("msg", "SERVICE GET SUCCESS");
        }
        msg.put("data", data);
        return msg;
    }

    /**
     * 错误信息
     *
     * @param errorCode 错误代码
     * @return 查询结果
     */
    private JSONObject assistMsg(int errorCode, Object data) {
        JSONObject msg = new JSONObject();
        msg.put("code", errorCode);
        if (errorCode == 404) {
            msg.put("msg", "SERVICE GET AN EXCEPTION");
        } else {
            msg.put("msg", "SERVICE GET SUCCESS");
        }
        msg.put("data", data);
        return msg;
    }
}
